package com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.persenter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.DevicesManagerAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.DevicesManagerPagerAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.DevicesCustomConfigurationBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.DevicesManagerBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.SetCurrentMeetingBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.inter.IonClickedItemListenter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.contract.IDevicesManagerPagerCantract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.logic.modle.DevicesManagerPagerModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.RecyclerViewUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/13.
 */

public class DevicesManagerPagerPersenter implements IDevicesManagerPagerCantract.IDevicesManagerPagerPersenter{
    private String TAG=DevicesManagerPagerPersenter.class.getSimpleName();
    private IDevicesManagerPagerCantract.IDevicesManagerPagerView iDevicesManagerPagerView;
    private Context mContext;
    private RecyclerView mRecyclerView;
    private DevicesManagerPagerAdapter devicesManagerPagerAdapter;
    private IDevicesManagerPagerCantract.IDevicesManagerPagerModle mIDevicesManagerPagerModle;
    private List<DevicesCustomConfigurationBean> mlist;
    private Handler handler = new Handler();

    public DevicesManagerPagerPersenter(IDevicesManagerPagerCantract.IDevicesManagerPagerView iDevicesManagerPagerView) {
        this.iDevicesManagerPagerView = iDevicesManagerPagerView;
        mIDevicesManagerPagerModle = new DevicesManagerPagerModle();
    }
    @Override
    public void initView(RecyclerView recyclerView, Context context) {
        mContext=context;
        mRecyclerView=recyclerView;
        devicesManagerPagerAdapter=new DevicesManagerPagerAdapter<DevicesCustomConfigurationBean>(mContext);
        RecyclerViewUtils recyclerViewUtils=new RecyclerViewUtils();
        recyclerViewUtils.initView(mRecyclerView,0,mContext,devicesManagerPagerAdapter);
        setDevicesManagerAdapterListener(devicesManagerPagerAdapter);

    }

    @Override
    public void loadDate(Context context, String id) {
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.GET_DEVICES_ATTRIB)
                .addHeader("sessionId",mIDevicesManagerPagerModle.getToken(context))
                .addParams("deviceId", id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                       // Log.e(TAG, "loadRecyclerViewDate:token ==sdg111===> "+ response.body().string());
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                    String normalDate = JsonUitl.getStringDateFeld(object);
                                   Type type = new TypeToken<List<DevicesCustomConfigurationBean>>(){}.getType();
                                    mlist = new Gson().fromJson(normalDate,type);
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            devicesManagerPagerAdapter.AddItemDate(mlist);
                                        }
                                    });
                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    @Override
    public void initDate(Context context, DevicesManagerBean bean) {
        if(bean != null){
          String name =   bean.getName();
          if(name != null && !TextUtils.isEmpty(name)){
              iDevicesManagerPagerView.setDeviceName(name);
              iDevicesManagerPagerView.toMettingName(name);
          }
            String model = bean.getModel();
            if(model != null && !TextUtils.isEmpty(model)){
                iDevicesManagerPagerView.setDeviceType(model);
            }
           int mouseNum =  bean.getMouseNum();
            iDevicesManagerPagerView.setDeviceMouseTotal(mouseNum+"个");
           double hardDiskSize =  bean.getHardDiskSize();
            iDevicesManagerPagerView.setDeviceStorage((int)hardDiskSize+"G");
            double localStoreUseSize =  bean.getLocalStoreUseSize();
            double progress = localStoreUseSize/hardDiskSize;
            iDevicesManagerPagerView.setMyProgress((int)(progress*100));
            iDevicesManagerPagerView.setProgressContent((int)(progress*100)+"%");
        }
    }

    @Override
    public void onClickedBackImageView() {
        iDevicesManagerPagerView.toback();

    }

    @Override
    public void onClickedHeadImageView() {

    }

    @Override
    public void onClickedChangeImageView() {

    }

    @Override
    public void onClickedManagerTextView() {
        iDevicesManagerPagerView.toManageProfilesPager();
    }

    /**
     * @desc 设置监听
     * */
    private void setDevicesManagerAdapterListener(DevicesManagerPagerAdapter managerAdapter) {
        managerAdapter.setmIonClickedItemListenter(new IonClickedItemListenter() {
            @Override
            public void onClickedItem(View view) {

            }

            @Override
            public void onClickedFist(int postion) {
               // Log.d(TAG, "onClickedFist: "+postion);
                iDevicesManagerPagerView.toFileSettingPager(postion);

            }

            @Override
            public void onClickedSecond(int postion) {
            }
        });
    }


}
